//
//  TDJScrollAdView.m
//  淘代驾
//
//  Created by trimaximus on 14-1-20.
//  Copyright (c) 2014年 muyekeji. All rights reserved.
//
#define TIMER_TIME 8

#import "TDJScrollAdView.h"

@interface TDJScrollAdView ()

@end

@implementation TDJScrollAdView
@synthesize delegate = _delegete;

@synthesize scrollView;
@synthesize pageControl;
@synthesize currentPage;
@synthesize totalPages;
@synthesize currentViews;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [scrollView setDelegate:self];
        [scrollView setContentSize:CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height)];
        [scrollView setShowsHorizontalScrollIndicator:NO];
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
        [scrollView setPagingEnabled:YES];
        [scrollView setBackgroundColor:[UIColor darkGrayColor]];
        [self addSubview:scrollView];
        
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100) / 2, scrollView.bounds.size.height - 30, 100, 30)];
        [pageControl setUserInteractionEnabled:NO];
        [pageControl setPageIndicatorTintColor:[UIColor blackColor]];
        [pageControl setCurrentPageIndicatorTintColor:[UIColor colorWithRed:241/255.0f green:158/255.0f blue:27/255.0f alpha:1]];
        
        [self addSubview:pageControl];
        
        currentPage = 0;
        
        self.timerScroll = [NSTimer scheduledTimerWithTimeInterval:TIMER_TIME target:self selector:@selector(changePic) userInfo:nil repeats:YES];
    }
    return self;
}


- (void)changePic
{
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width + [UIScreen mainScreen].bounds.size.width, 0) animated:YES];
    [self loadData];
}

- (void)reloadData
{
    totalPages = [self.dataSource numberOfPages];
    if(totalPages == 0) {
        return;
    }
    
    pageControl.numberOfPages = totalPages;
    [self loadData];
}

- (void)loadData
{
    pageControl.currentPage = currentPage;
    
    //从scrollView上移除所有子视图
    NSArray *arrSubViews = [scrollView subviews];
    if([arrSubViews count]) {
        [arrSubViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    [self getDisplayImagesWithCurrentPage:currentPage];
    for(int i = 0; i < 3; i++)
    {
        UIView *view = [currentViews objectAtIndex:i];
        [view setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnScrollView:)];
        [view addGestureRecognizer:tapGesture];
        [view setFrame:CGRectOffset(view.frame, view.frame.size.width * i, 0)];
        [scrollView addSubview:view];
    }
    
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)];
}

- (void)getDisplayImagesWithCurrentPage:(NSInteger)pageIndex
{
    NSInteger preIndex = [self validPageValue:currentPage - 1];
    NSInteger postIndex = [self validPageValue:currentPage + 1];
    if(!currentViews) {
        currentViews = [[NSMutableArray alloc] init];
    }
    
    [currentViews removeAllObjects];
    
    @try {
        [currentViews addObject:[self.dataSource pageAtIndex:preIndex]];
        [currentViews addObject:[self.dataSource pageAtIndex:pageIndex]];
        [currentViews addObject:[self.dataSource pageAtIndex:postIndex]];
    }
    @catch (NSException *exception) {}
    @finally {}
    
}

- (NSInteger)validPageValue:(NSInteger)value
{
    if(value == -1) {
        value = totalPages - 1;
    }
    
    if(value == totalPages) {
        value = 0;
    }
    
    return value;
}

- (void)setViewContent:(UIView *)view atIndex:(NSInteger)index
{
    if(index == currentPage)
    {
        [currentViews replaceObjectAtIndex:1 withObject:view];
        for (int i = 0; i < 3; i++)
        {
            UIView *view = [currentViews objectAtIndex:i];
            [view setUserInteractionEnabled:YES];
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnScrollView:)];
            [view addGestureRecognizer:tapGesture];
            [view setFrame:CGRectOffset(view.frame, view.frame.size.width * i, 0)];
            [scrollView addSubview:view];
        }
    }
}

- (void)tapOnScrollView:(UITapGestureRecognizer *)aTapGestureRecognizer
{
    if([self.delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
        [self.delegate didClickPage:self atIndex:currentPage];
    }
}


#pragma mark-
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    NSInteger x = aScrollView.contentOffset.x;
    if(x >= 2 * self.frame.size.width)
    {
        currentPage = [self validPageValue:currentPage + 1];
        [self loadData];
    }
    
    if(x <= 0)
    {
        currentPage = [self validPageValue:currentPage - 1];
        [self loadData];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:YES];
}

- (void)dealloc {
    [self.timerScroll invalidate];
}

@end
